<template>
  <div class="visitor-container">
    <div class="top-item-group">
      <el-select v-model="serviceValue" clearable placeholder="客户等级">
        <el-option
          v-for="item in serviceOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="sourceValue" clearable placeholder="全部来源">
        <el-option
          v-for="item in sourceOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input placeholder="请输入关键词搜索" v-model="searchKeyword" clearable></el-input>
      <el-button type="primary">搜索</el-button>
      <el-button style="float:right;">导出数据</el-button>
      <el-button type="primary" style="float:right;" @click="handlePartialDelete">批量解禁</el-button>
    </div>
    <div class="middle-table-group">
      <el-table
        :header-cell-style="{'background-color':'#e6f1ff','height':'62px'}"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        v-loading="isLoading"
        @selection-change="handleSelectionChange"
        :cell-style="cellStyle"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="customerName" label="客户名称" align="center"></el-table-column>
        <el-table-column prop="realName" label="真实姓名" align="center"></el-table-column>
        <el-table-column prop="phone" label="电话" align="center"></el-table-column>
        <el-table-column prop="email" label="邮箱" align="center"></el-table-column>
        <el-table-column prop="level" label="客户等级" align="center"></el-table-column>
        <el-table-column prop="channel" label="客户来源" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="getDetail(scope.$index, tableData)" type="text" size="normal">详情</el-button>
            <el-button @click="deleteCustomer(scope.$index, tableData)" type="text" size="normal">解禁</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="bottom-pagination-group">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes, jumper"
        :total="totalData"
      ></el-pagination>
    </div>

    <el-dialog title="解禁确认" :visible.sync="unbanDialogVisible" top="34vh" width="26%">
      <div class="unban-dialog-style">确定要解禁该客户吗？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="unbanDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleDelete">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import api from "@/api/api";
export default {
  name: "SettingBlacklist",
  components: {},
  props: {},
  data() {
    return {
      tableData: [],
      sourceValue: "",
      searchKeyword: "",
      multipleSelection: [],
      unbanDialogVisible: false,
      currentPage: 1,
      pageSize: 10,
      totalData: 0,
      isLoading: false,
      deleteId:0
    };
  },
  watch: {},
  computed: {},
  methods: {
    handleSizeChange(size) {
      this.pageSize = size;
      this.getPageData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getPageData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getDetail(index, tableData) {
      this.$router.push({
        name: "customer-detail",
        params: tableData[index]
      });
    },
    deleteCustomer(index,tableData) {
      this.unbanDialogVisible = true;
      this.deleteId = tableData[index].id;
    },
    handlePartialDelete() {
      let params = this.multipleSelection.map(item => {
        return item.id;
      });
      params = params.join(",");
      api.partialDeleteBlackListAPI(params).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.multipleSelection = [];
          this.getPageData();
          this.$message({
            message: "解禁成功",
            type: "success"
          });
        }
      });
    },
    handleDelete(){
      this.unbanDialogVisible = false;
      api.deleteBlackListAPI(this.deleteId).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.getPageData();
          this.$message({
            message: "解禁成功",
            type: "success"
          });
        }
      });
    },
    getPageData() {
      this.isLoading = true;
      api
        .getAllBlackListAPI({
          page: this.currentPage,
          limit: this.pageSize
        })
        .then(res => {
          this.isLoading = false;
          let { error, data } = res.data;
          if (error) {
            this.$message.error("请求接口异常");
          } else {
            this.totalData = data.total;
            this.tableData = data.page;
          }
        });
    }
  },
  created() {
    this.getPageData();
  },
  mounted() {}
};
</script>

<style scoped>
.top-item-group {
  padding-bottom: 20px;
}
.visitor-container {
  /* margin: 20px; */
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
}
.bottom-pagination-group {
  padding: 20px 20px 0;
  padding-left: 550px;
}
.el-select {
  width: 162px;
}
.el-input {
  width: 200px;
}

.unban-dialog-style {
  border-top: 1px solid #f2f2f2;
  border-bottom: 1px solid #f2f2f2;
  padding: 20px;
  padding-bottom: 70px;
}
</style>
